﻿using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Jn698._45GWTool.Models.DataGrid;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels
{
    public class AddCollectArViewModel:ViewModelBase
    {
        public static GWCmdMakeUp GWCmdMakeUp;
        public static GWCmdMakeUpPlan GWCmdMakeUpPlan;

        private string address;

		public string Address
		{
			get { return address; }
			set { address = value; RaisePropertyChanged(); }
		}

		private string password;

		public string Password
		{
			get { return password; }
			set { password = value; RaisePropertyChanged(); }
		}

		private ObservableCollection<string> baudRateList;

		public ObservableCollection<string> BaudRateList
        {
			get { return baudRateList; }
			set { baudRateList = value; RaisePropertyChanged(); }
		}

		private int rateNum;

		public int RateNum
		{
			get { return rateNum; }
			set { rateNum = value; RaisePropertyChanged(); }
		}

		private ObservableCollection<string> agreementTypeList;

		public ObservableCollection<string> AgreementTypeList
		{
			get { return agreementTypeList; }
			set { agreementTypeList = value; RaisePropertyChanged(); }
		}

		private ObservableCollection<string> portList;

		public ObservableCollection<string> PortList
		{
			get { return portList; }
			set { portList = value; RaisePropertyChanged(); }
		}

		private ObservableCollection<string> connectTypeList;

		public ObservableCollection<string> ConnectTypeList
		{
			get { return connectTypeList; }
			set { connectTypeList = value; RaisePropertyChanged(); }
		}

		private int userType;

		public int UserType
		{
			get { return userType; }
			set { userType = value; RaisePropertyChanged(); }
		}

		private double voltage;

		public double Voltage
		{
			get { return voltage; }
			set { voltage = value; RaisePropertyChanged(); }
		}

		private double current;

		public double Current
		{
			get { return current; }
			set { current = value; RaisePropertyChanged(); }
		}

		private int baudRateSelected;

		public int BaudRateSlected
        {
			get { return baudRateSelected; }
			set { baudRateSelected = value; RaisePropertyChanged(); }
		}

        private int agreementTypeSelected;

        public int AgreementTypeSlected
        {
            get { return agreementTypeSelected; }
            set { agreementTypeSelected = value; RaisePropertyChanged(); }
        }

		private int portSelected;

		public int PortSelected
		{
			get { return portSelected; }
			set { portSelected = value; RaisePropertyChanged(); }
		}

		private int connectTypeSelected;

		public int ConnectTypeSelected
        {
			get { return connectTypeSelected; }
			set { connectTypeSelected = value; RaisePropertyChanged(); }
		}


		public AddCollectArViewModel()
        {
			BaudRateList = new ObservableCollection<string>() { "300","600","1200","2400","4800","7200","9600","19200","38400","57600","115200","自适应" };
			AgreementTypeList = new ObservableCollection<string>() { 
				"DL/T 645-1997" ,
				"DL/T 645-2007",
                "本部分",
                "CT/T 188-2004",
            };
            ConnectTypeList = new ObservableCollection<string>() { "未知","单相","三相三线","三相四线"};
			PortList = new ObservableCollection<string>() { "交采","485-1","485-2","485-3","载波"};
			string[] datas;
            if (MainViewModel.TopIndex == 0)
            {
                GWCmdMakeUp = ServiceLocator.Current.GetInstance<GWCmdMakeUp>();
                if (GWCmdMakeUp.SubItemName == null)
                {
                    return;
                }
                datas = GWCmdMakeUp.DataValue.Split(',');
            }
            else
            {
                GWCmdMakeUpPlan = ServiceLocator.Current.GetInstance<GWCmdMakeUpPlan>();
                if (GWCmdMakeUpPlan.SubItemName == null)
                {
                    return;
                }
                datas = GWCmdMakeUpPlan.DataValue.Split(',');
            }

            
			Address = datas[0];
			BaudRateSlected = Convert.ToInt16(datas[1]);
			AgreementTypeSlected = Convert.ToInt16(datas[2]);
			ConnectTypeSelected = Convert.ToInt16(datas[7]);
			switch (datas[3])
			{
				case "F2080201":
					PortSelected = 0;
					break;
                case "F2010201":
                    PortSelected = 1;
                    break;
                case "F2010202":
                    PortSelected = 2;
                    break;
                case "F2010203":
                    PortSelected = 3;
                    break;
                case "F2090201":
                    PortSelected = 4;
                    break;
                default:
					break;
			}
			Password = datas[4];
			RateNum = Convert.ToInt16(datas[5]);
			UserType= Convert.ToInt16(datas[6]);
			Voltage = double.Parse(datas[8]);
			Current = double.Parse(datas[9]);
        }

		public RelayCommand<Window> OKCommand
		{
			get
			{
				var command = new RelayCommand<Window>((w) =>
				{
					StringBuilder stringBuilder = new StringBuilder();
					stringBuilder.Append(Address);
					stringBuilder.Append(",");
					stringBuilder.Append(BaudRateSlected.ToString());
					stringBuilder.Append(",");
					stringBuilder.Append(AgreementTypeSlected.ToString());
					stringBuilder.Append(",");
					switch (PortSelected)
					{
						case 0:
							stringBuilder.Append("F2080201");
                            break;
						case 1:
                            stringBuilder.Append("F2010201");
                            break;
						case 2:
                            stringBuilder.Append("F2010202");
                            break;
						case 3:
                            stringBuilder.Append("F2010203");
                            break;
						case 4:
                            stringBuilder.Append("F2090201");
                            break;
						default:
							break;
					}
                    stringBuilder.Append(",");
                    stringBuilder.Append(Password);
                    stringBuilder.Append(",");
                    stringBuilder.Append(RateNum.ToString());
                    stringBuilder.Append(",");
                    stringBuilder.Append(UserType.ToString());
                    stringBuilder.Append(",");
                    stringBuilder.Append(ConnectTypeSelected.ToString());
                    stringBuilder.Append(",");
                    stringBuilder.Append(Voltage.ToString());
                    stringBuilder.Append(",");
                    stringBuilder.Append(Current.ToString());
                    if (MainViewModel.TopIndex == 0)
                    {
                        GWCmdMakeUp.DataValue = stringBuilder.ToString();
                    }
                    else
                    {
                        GWCmdMakeUpPlan.DataValue = stringBuilder.ToString();
                    }
                    
                    w.DialogResult = true;
				});
				return command;
			}
		}

        public RelayCommand<Window> CloseCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {
                    w.DialogResult = false;
                });
                return command;
            }
        }
    }
}
